var uDumper = function(){
	
	var jDumped = false;
	var appDIR = Ti.Filesystem.applicationDataDirectory;
	
	this.setup = function(){
		
		var new_folder = Ti.Filesystem.getFile(appDIR, 'data');
		if(!new_folder.exists())
		{
			new_folder.createDirectory();
		}
		var new_folder = Ti.Filesystem.getFile(appDIR, 'media');
		if(!new_folder.exists())
		{
			new_folder.createDirectory();
		}
		
		var f = Ti.Filesystem.getFile(appDIR, 'data/version.js');
	   	f.write("0");
		
	    
		if(f.exists()){
			var v = f.read().text;
			if(checkNetwork()){
				checkUpdate(v);
			}
			jDumped = true;
		}
		
		
		return jDumped;
	}
	
	
	this.goUpdate = function(v){
		if(checkNetwork()){
			checkUpdate(v);
		}
		
	}

	var getArray = function(remoteFiles){
		
		var dataArray = [];
		
		for(rF in remoteFiles){
			var rootLevel = remoteFiles[rF];
			for(rL in rootLevel){
				var topLevel = rootLevel[rL];
				for(tL in topLevel.content){
					var secondLevel = topLevel.content[tL];
					if(secondLevel.img){
						dataArray.push(secondLevel.img);		
					}
					
					for(sL in secondLevel.content){
							var lastLevel = secondLevel.content[sL];
							if(secondLevel.content.img){
								dataArray.push(secondLevel.content.img);	
							}
							if(lastLevel.img){
								dataArray.push(lastLevel.img);	
							}
					}
					
					
				}
					
			}
		}
		
		//Ti.API.debug(dataArray);
		return dataArray;
	}
	
	var saveIT = function(pLocation, pFile){
		
		var filesArray = [pFile+"0.png", pFile+"0@2x.png", pFile+"1.png", pFile+"1@2x.png", pFile+"2.jpg", pFile+"2@2x.jpg", pFile];
		if(pLocation == 'remote'){
			for(f in filesArray){
				saveRemote("http://www.xinyilian.cn/apps/"+filesArray[f]);
			}	
		}
		
		if(pLocation == 'local'){
			for(f in filesArray){
				var tmpFile = Titanium.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, filesArray[f]);
				saveData(tmpFile.read(), filesArray[f]);
			}
		}
		
	}
	
	
	var doUpdate = function(pUpdate){

		for(pU in pUpdate){
			saveIT("remote", pUpdate[pU]);
		}
		
	}
	
	var checkUpdate = function(cVerion){
		
		var jLoader = Ti.Network.createHTTPClient();
		jLoader.onload = function(e) {
			if (jLoader.status == 200){
				var jsonData = JSON.parse(jLoader.responseData);
				if(jsonData[5].Status.version == cVerion){
					Titanium.UI.iPhone.appBadge = null;
				}
				
				if(jsonData[5].Status.version != cVerion){
					Titanium.UI.iPhone.appBadge = jsonData[5].Status.version;
					
					var dataFile = Ti.Filesystem.getFile(appDIR, 'data/data.js');
					dataFile.write(jLoader.responseData);
					var vFile = Ti.Filesystem.getFile(appDIR, 'data/version.js');
					vFile.write(jsonData[5].Status.version);
					
					doUpdate(jsonData[5].Status.content, jLoader.responseData);
		
				}
								
			}else{
				//Ti.API.debug("fail---"+pURL);
				
			}
		}
		jLoader.open('GET', 'http://www.xinyilian.cn/apps/outbox/feed/v12.js');
		jLoader.send();
		
		
	}
	
	var saveData = function(fileData, pFolder){
	
		var folderArray = pFolder.split("/");
		var pFile = folderArray[folderArray.length-1];
		
		var sFolder = "";
		var sFolderArray = folderArray.slice(0, folderArray.length-1);
		for(k in sFolderArray){
			sFolder = sFolder + sFolderArray[k] + "/";
		}
		
		for (var f = 0; f < folderArray.length-1; f++){
			
			if(f == 0){
				var newFolder = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, folderArray[f]);
			}
			if(f == 1){
				var newFolder = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, folderArray[f-1]+"/"+folderArray[f]);
			}
			
			if(f == 2){
				var newFolder = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, folderArray[f-2]+"/"+folderArray[f-1]+"/"+folderArray[f]);
			}
			
			newFolder.createDirectory();
		}
		
		var fileFolder = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, sFolder);
		Titanium.Filesystem.getFile(fileFolder.nativePath, pFile).write(fileData);
		
		jDumped = true;	
	}
	
	var saveRemote = function(pURL){
		
		var jLoader = Ti.Network.createHTTPClient();
		jLoader.onload = function(e) {
			if (jLoader.status == 200){
				var folderString = pURL.substring(29);
				saveData(jLoader.responseData, folderString);
								
			}else{
				//Ti.API.debug("fail---"+pURL);
				
			}
		}
		
		jLoader.open('GET', pURL);
		jLoader.send();
		
	}
	
	
}
